package org.groovyflow.spring.web;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.WebApplicationContextUtils;

import zero.core.context.Context;
import zero.core.context.GlobalContext;

import org.groovyflow.aop.*;


//TODO Add a hook where developers can do Groovy things such as adding 
//methods to classes.
public class GroovyFlowContextLoaderListener extends ContextLoaderListener {
	
	public void contextInitialized(ServletContextEvent servletContextEvent) {
		super.contextInitialized(servletContextEvent);
		//Wakes up our BeanLocator
		ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContextEvent.getServletContext());
		ctx.getBean("beanLocator");
		
		//This allows the limited use we make of Project Zero Json.
		//If we ever replace that with something better (and it's pretty weak)
		//we need to remember to get rid of this.
        GlobalContext.setContext(new Context());
        
        StartupAop.exe();
        
        ServletContextHolder.servletContext = servletContextEvent.getServletContext();
	}
	
	public void contextDestroyed(ServletContextEvent event) {
		ServletContextHolder.servletContext = null;
	}	

}
